package com.et.machine.manager.vm.setting

import android.util.Log
import androidx.lifecycle.ViewModel
import com.et.api.MachineConfig
import com.et.drive.info.*
import com.et.machine.convert.lightTemperatureInfo
import com.et.machine.convert.machineIdInfo
import com.et.machine.convert.machineInfo
import kotlin.math.ceil

class SettingViewModel : ViewModel() {

    private val foodNumber =
        (machineInfo?.nFood!!) +
                (machineIdInfo?.nFood2!!) +
                (machineIdInfo?.nFood3!!) +
                (machineIdInfo?.nFood4!!)

    fun settingCoolTemperature(box: Int, coolTemp: Int) {
        lightTemperatureInfo[box]?.let {
            it.settingCoolTemperature = coolTemp
            sendLightTemperatureInfo(it)
        }
    }


    fun settingHotTemperature(box: Int, hotTemp: Int) {
        lightTemperatureInfo[box]?.let {
            it.settingHotTemperature = hotTemp
            sendLightTemperatureInfo(it)
        }
    }

    fun setEnergySaveTime(box: Int, time: Time) {
        lightTemperatureInfo[box]?.let {
            for (value in lightTemperatureInfo.values) {
                value.energySavingTime = time
            }
            sendLightTemperatureInfo(it)
        }
    }

    fun setOpenLightTime(box: Int, time: Time) {
        lightTemperatureInfo[box]?.let {
            for (value in lightTemperatureInfo.values) {
                value.openLightTime = time
            }
            sendLightTemperatureInfo(it)
        }
    }

    fun setLightStatus(box: Int, lightStatus: LightStatusEnum) {
        lightTemperatureInfo[box]?.let {
            it.lightStatus = lightStatus
            sendLightTemperatureInfo(it)
        }
    }

    fun setLeftStatus(box: Int, leftRoomStatus: TemperatureStateEnum) {
        lightTemperatureInfo[box]?.let {
            it.leftRoomStatus = leftRoomStatus
            sendLightTemperatureInfo(it)
        }
    }

    fun setRightStatus(box: Int, leftRoomStatus: TemperatureStateEnum) {
        lightTemperatureInfo[box]?.let {
            it.rightRoomStatus = leftRoomStatus
            sendLightTemperatureInfo(it)
        }
    }


    private fun sendLightTemperatureInfo(lightTemperatureInfo: LightTemperatureInfo) {
        MachineConfig.setSystemState(
            lightSenior = true,
            notDetectGoods = false,
            lightTemperatureInfo = lightTemperatureInfo
        )
    }


    /**
     * box box count
     */
    fun addBoxList(): MutableList<Box> {
        val boxData = mutableListOf<Box>()
        Log.d(
            "MachineIdInfo", "nFood:${machineInfo?.nFood!!}, " +
                    "nFood2:${(machineIdInfo?.nFood2!!)}, " +
                    "nFood3:${(machineIdInfo?.nFood3!!)}, " +
                    "nFood4:${(machineIdInfo?.nFood4!!)}"
        )

        if (machineInfo?.cDrink!! > 0) {
            boxData.add(Box(false, BOX_11))
        }
        for (i in 9 downTo 10 - ceil(foodNumber / 48.0).toInt()) {
            boxData.add(Box(false, i))
        }
        return boxData
    }


    fun box2Name(id: Int): String {
        return when (id) {
            BOX_11 -> "$id 饮料机"
            BOX_06, BOX_07, BOX_08, BOX_09 -> "$id 食品机"
            BOX_01, BOX_02, BOX_03, BOX_04, BOX_05 -> "$id 格子柜"
            else -> "unknown"
        }
    }

    companion object {

        data class Box(var select: Boolean, val box: Int)

        const val BOX_11 = 11
        const val BOX_09 = 9
        const val BOX_08 = 8
        const val BOX_07 = 7
        const val BOX_06 = 6
        const val BOX_01 = 1
        const val BOX_02 = 2
        const val BOX_03 = 3
        const val BOX_04 = 4
        const val BOX_05 = 5
    }

}